package leetcode.part3;

import org.junit.Test;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class S205 {
    public boolean isIsomorphic(String s, String t) {
        if (s.length() != t.length()) return false;
        Map<Character, Character> map = new HashMap<>();
        for (int i = 0; i < s.toCharArray().length; i++) {
            if (map.containsKey(s.charAt(i))) {
                if (map.get(s.charAt(i)) != t.charAt(i)) {
                    return false;
                }
                continue;
            }
            // 判断当前字符对应的t中的字符已经和s前面的某个字符建立过映射关系
            if (map.containsValue(t.charAt(i))) return false;

            map.put(s.charAt(i), t.charAt(i));

        }
        return true;
    }

    @Test
    public void test() {
        String s = "abba", t = "cddc";
        String s1 = "foo", t1 = "bar";
        String s2 = "paper", t2 = "title";
        System.out.println(isIsomorphic(s, t));
        System.out.println(isIsomorphic(s1,t1));
        System.out.println(isIsomorphic(s2,t2));
    }
}
